<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style/main.css" type="text/css" rel="stylesheet">
        <link href="style/Search.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="script/jquery-1.8.2.js"></script>
        <script type="text/javascript" src="script/js1.js"></script>
        <script type="text/javascript" src="script/js2.js"></script>
        <title></title>
        
        
    </head>
    <body onload ="radioactivate()">
    <header>
        
        <?php
        session_start();
        mysql_connect("localhost:3306", "root", "16886611");
        mysql_select_db("digitalsales");
        if(isset($_GET["action"])){
            if($_GET["action"]=="logout"){
               $_SESSION["username"]=NULL;
            }
        }
        
        if(!isset($_SESSION["username"])||$_SESSION["username"]==NULL){
        echo '<ul>
                <li id="login">
                  <p>Log in</p>
                </li>
                 <li>
                    <p><a href="../PhpProject1/index.php?action=logout">Register</a></p>
                  </li>
                </ul>';
        }else{
            echo '<ul>
                <li id="logout">
                  <p><a href="../PhpProject1/index.php?action=logout">Log out</a></p>
                </li>
                 <li>
                    <p><a href="../PhpProject1/index.php?action=logout">My Account</a></p>
                  </li>
                  <li>
                    <p><a href="../PhpProject1/index.php?action=logout">My Orders</a></p>
                  </li>
                  <li>
                    <p>Welcome back,'.$_SESSION["username"].'!</p>
                  </li>
                </ul>';
        }
        
        
        ?>
        </header>
        
       <?php
           
           if (!isset($_GET['myself'])){
               foreach ($_COOKIE as $key=>$value){
                   unset($_COOKIE[$key]);
                    setcookie($key,NULL,-1);
               }
           }
           
           if (isset($_GET['category'])){
               setcookie ('category',$_GET['category']);
           }
           if (isset($_GET['text'])){
               setcookie('text',$_GET['text']);
           }

       ?>
          
        
          <div id="panel1" class="Mainpanel">
            <div id="logo">
                
                <a><img src="images/log.png"></a>
            </div>
            <div id="search">
                <form name="form2" method="GET" action="search.php">
                <input id ="textAr" type="text" name="text" class="textArea" <?php if(isset($_COOKIE["text"])) echo 'value="'.$_COOKIE["text"].'"';
                else if(isset($_GET["text"])) echo 'value="'.$_GET["text"].'"';?> />
                    <input type="submit" class="searchButton" value="Search" /><br/>
                </form>
            </div>
            <div style="clear:both;"></div>
          </div>
        
        
        
   
        
        
             <div class="Mainpanel">
             <div class="conditionList">
               
             <label>Price Range:</label><br/>
             
             <table>
             <tbody>
             <tr><td><input title = "inp" onclick="jump1('pricerange=Any')" type="radio" name="pricerange" value ="ANY" id ="ANY" >ANY</td></tr>
             <tr><td><input title = "inp" onclick="jump1('pricerange=0-100')" type="radio" name="pricerange" value ="0-100" id ="0-100" >0-100</td></tr>
             <tr><td><input title = "inp" onclick="jump1('pricerange=100-200')" type="radio" name="pricerange" value ="100-200" id ="100-200">100-200</td></tr>
             <tr><td><input title = "inp" onclick="jump1('pricerange=200-300')" type="radio" name="pricerange" value ="200-300" id ="200-300" >200-300</td></tr>
             <tr><td><input title = "inp" onclick="jump1('pricerange=300-1000')" type="radio" name="pricerange" value ="300-1000" id ="300-1000">300-1000</td></tr>
             <tr><td><input title = "inp" onclick="jump1('pricerange=1000-100000000000000')" type="radio" name="pricerange" value ="1000-100000000000000" id ="1000-100000000000000">1000+<br/>
             </tbody>
             </table>
        
             
             <label>Regions:</label>
                <table id ="radioregion">
                    <tbody>
                        <tr><td><input onclick="jump1('region=Any')" type="radio" name="region" value ="ANY" id ="ANY1" title = "inp">ANY</td></tr>
                        <tr><td><input onclick="jump1('region=PITTSBURGH')" type="radio" name="region" value ="PITTSBURGH" id ="PITTSBURGH" title = "inp">PITTSBURGH</td></tr>
                        <tr><td><input onclick="jump1('region=PHILADELPHIA')" type="radio" name="region" value ="PHILADELPHIA" id ="PHILADELPHIA" title = "inp">PHILADELPHIA</td></tr>
                        <tr><td><input onclick="jump1('region=SCRANTON')" type="radio" name="region" value ="SCRANTON" id ="SCRANTON" title = "inp">SCRANTON</td></tr>
                        <tr><td><input onclick="jump1('region=YORK')" type="radio" name="region" value ="YORK" id ="YORK" title = "inp">YORK</td></tr>
                        <tr><td><input onclick="jump1('region=WILLIAMPORT')" type="radio" name="region" value ="WILLIAMPORT" id ="WILLIAMPORT" title = "inp">WILLIAMPORT</td></tr>                
                    </tbody>
                </table>
              
           
           <script type="text/javascript" >  
  
            function auxiFuction(){  
                var evt=window.event  ;  
                 var selectObj=evt?evt.srcElement:null;  
    
                if (evt && selectObj &&  evt.offsetY && evt.button!=2  
                   && (evt.offsetY > selectObj.offsetHeight || evt.offsetY<0 ) ) {    
                    var oldIdx = selectObj.selectedIndex;  
  
                    setTimeout(function(){  
                    var option=selectObj.options[selectObj.selectedIndex];    
                    showOptionValue(option)}, 60);  
                    }  
            }    
           </script>
                
                <label>Stores:</label>
                
                <select id="selectID" name ="selectID" onclick ="auxifunction()">
                           
                </select>
            
                
                
            <?php  
                  if (isset($_GET['region'])){
                     echo "<Script language='JavaScript'> test('".$_GET['region']."');</Script>";
                  }
                  else if (isset($_COOKIE['region']))
             {
                  echo "<Script language='JavaScript'> test('".$_COOKIE['region']."');</Script>";
             }
             
             ?>   
             

                
 
           
             
             
             
             
             <label>Category:</label>
                <table id ="radiocategory">
                    <tbody>
                        <tr><td><input onclick="jump1('category=Any')" type="radio" name="category" value ="ANY" id = "ANY" title = "inp">ANY</td></tr>
                        <tr><td><input onclick="jump1('category=BOOKS')" type="radio" name="category" value ="BOOKS" id = "BOOKS" title = "inp">BOOKS</td></tr>
                        <tr><td><input onclick="jump1('category=CLOTHES')" type="radio" name="category" value ="CLOTHES" id = "CLOTHES" title = "inp">CLOTHES</td></tr>
                        <tr><td><input onclick="jump1('category=MACHINE')" type="radio" name="category" value ="MACHINE" id = "MACHINE" title = "inp">MACHINE</td></tr>
                        <tr><td><input onclick="jump1('category=MEDICINE')" type="radio" name="category" value ="MEDICINE" id = "MEDICINE" title = "inp">MEDICINE</td></tr>
                        <tr><td><input onclick="jump1('category=FOOD')" type="radio" name="category" value ="FOOD" id = "FOOD" title = "inp">FOOD</td></tr>
                        <tr><td><input onclick="jump1('category=TOOLS')" type="radio" name="category" value ="TOOLS" id = "TOOLS" title = "inp">TOOLS</td></tr>
                    </tbody>
                </table>
              


                <label>SubCategory:</label>
                
                <select id="SubID" name ="SubID" onclick ="auxifunction">
                      
                </select>
                
                <?php  
                  if (isset($_GET['category'])){
                      echo "<Script language='JavaScript'> test1('".$_GET['category']."');</Script>";
                  }
                  else if (isset($_COOKIE['category']))
                  {
                  echo "<Script language='JavaScript'> test1('".$_COOKIE['category']."');</Script>";
                  }
             
             ?> 
                
            </div>
        
            <div class="resultList">
                <div class="resultitems">
         

        <?php
           
            $auxi;
            $str1;
            $auxi2;
            $storeused=0;
            $catused2 =0;
            $cateVal;
            $textVal;
            
             
             
             //this part deals querys which contains only category or text
             $finalsql="";
             if(isset($_GET['text'])||isset($_GET["category"])){
                 $hasCondition=true;
                 $wherePart=" BigSp.pid=BigP.productid";
                 $simpleBase="select count(BigP.productid) from products BigP,product_stores BigSp";
                 if(isset($_GET['text'])){
                     $wherePart=$wherePart." and BigP.name LIKE '%".$_GET['text']."%'";
                     
                 }
                if(isset($_GET['category'])){
                    $wherePart=$wherePart." and BigP.product_type='".$_GET['category']."'";
                }
                 $finalsql=$simpleBase." where ".$wherePart;
             }else{
             
             
                    //$sqlres=mysql_query($sqlstr);
                    $hasCondition=false;//part1: sql for product table
                    $fromPart="";
                    $wherePart="";
                    $basesql_product="select BigP.productid from products BigP";

                    if(isset($_COOKIE['text'])||isset($_GET["text"])){
                        $hasCondition=true;
                        $text="";
                        if(isset($_COOKIE['text'])){
                            $text=$_COOKIE['text'];
                        }else{
                            $text=$_GET['text'];
                        }
                        $wherePart=$wherePart." BigP.name LIKE '%".$text."%'";
                        if(isset($_COOKIE["category"])||isset($_GET["category"])){
                            $category="";
                            if(isset($_COOKIE["category"])){
                                $category=$_COOKIE["category"];
                            }else{
                                $category=$_GET["category"];
                            }
                            $fromPart=",(select pt1.product_type from product_types pt1 where pt1.product_type='".$category."' or pt1.parent_type='".$category."') types";
                            $wherePart=$wherePart." and BigP.product_type=types.product_type";
                        }
                    }else{
                        if(isset($_COOKIE["category"])||isset($_GET["category"])){
                            $hasCondition=true;
                            $category="";
                            if(isset($_COOKIE["category"])){
                                $category=$_COOKIE["category"];
                            }else{
                                $category=$_GET["category"];
                            }
                            $fromPart=",(select pt1.product_type from product_types pt1 where pt1.product_type='".$category."' or pt1.parent_type='".$category."') types";
                            $wherePart=$wherePart." BigP.product_type=types.product_type";
                        }
                    }
                    $sql_product=$basesql_product;
             
             if($hasCondition){
                $sql_product=$basesql_product.$fromPart." where ".$wherePart;
             }
            
             $hasCondition2=false;
             $fromPart2="";
             $wherePart2="";
             $basesql_sp="select BigSp.pid from product_stores BigSp";
             if(isset($_COOKIE["pricerange"])){
                 $hasCondition2=true;
                 $temp = explode('-',$_COOKIE["pricerange"]);
                 $wherePart2=$wherePart2." BigSp.price between ".$temp[0]." and ".$temp[1];
                 if(isset($_COOKIE["store"])){
                     $wherePart2=$wherePart2." and BigSp.sid='".$_COOKIE["store"]."'";
                 }else{
                     if(isset($_COOKIE["region"])){
                         $fromPart2=",(select s1.storeid from stores s1 where s1.region='".$_COOKIE["region"]."') targets";
                         $wherePart2=$wherePart2." and BigSp.sid=targets.storeid";
                     }
                 }
             }else{
                 if(isset($_COOKIE["store"])){
                     $hasCondition2=true;
                     $wherePart2=$wherePart2." BigSp.sid='".$_COOKIE["store"]."'";
                 }else{
                     if(isset($_COOKIE["region"])){
                         $hasCondition2=true;
                         $fromPart2=",(select s1.storeid from stores s1 where s1.region='".$_COOKIE["region"]."') targets";
                         $wherePart2=$wherePart2." BigSp.sid=targets.storeid";
                     }
                 }
             }
             $sql_sp=$basesql_sp;
             if($hasCondition2){
                 $sql_sp=$basesql_sp.$fromPart2." where ".$wherePart2;
             }
             
             $finalsql="select count(*) from (".$sql_product.") sproducts1,(".$sql_sp.") sproducts2 where sproducts1.productid=sproducts2.pid";
             }
             global $num4;
             $sqlres=mysql_query($finalsql);
              while ($ress=mysql_fetch_row($sqlres)){
                 $num4=$ress[0] ;
              }
             
             
             
             $pagestart = 0;
             $pageend = 5;
             
             if(isset($_GET['page'])){
                 $pagenum = $_GET['page'];
                 $pagese = explode('-',$pagenum);
                 $pagestart = $pagese[0];
             }
            
                
                
                //------------------------------------------------
             $finalsql="";
             if(isset($_GET['text'])||isset($_GET["category"])){
                 $hasCondition=true;
                 $wherePart=" BigSp.pid=BigP.productid and s.storeid=BigSp.sid";
                 $simpleBase="select BigP.name as p1,BigP.picture as p2,BigP.product_type as p3,BigP.description as p4,BigSp.amount as p5,s.region,s.name,BigSp.price as p7,BigP.PRODUCTID as p8
                     from products BigP,product_stores BigSp, stores s";
                 if(isset($_GET['text'])){
                     $wherePart=$wherePart." and BigP.name LIKE '%".$_GET['text']."%'";
                     
                 }
                if(isset($_GET['category'])){
                    $wherePart=$wherePart." and BigP.product_type='".$_GET['category']."'";
                }
                 $finalsql=$simpleBase." where ".$wherePart."limit ".$pagestart.','.$pageend .";";
             }else{
                $hasCondition=false;//part1: sql for product table
                $fromPart="";
                $wherePart="";
                $basesql_product="select BigP.productid,BigP.name,BigP.picture,BigP.product_type,BigP.description from products BigP";
                if(isset($_COOKIE['text'])||isset($_GET["text"])){
                    $hasCondition=true;
                    $text="";
                    if(isset($_COOKIE['text'])){
                        $text=$_COOKIE['text'];
                    }else{
                        $text=$_GET['text'];
                    }
                    $wherePart=$wherePart." BigP.name LIKE '%".$text."%'";
                    if(isset($_COOKIE["category"])||isset($_GET["category"])){
                        $category="";
                        if(isset($_COOKIE["category"])){
                            $category=$_COOKIE["category"];
                        }else{
                            $category=$_GET["category"];
                        }
                        $fromPart=",(select pt1.product_type from product_types pt1 where pt1.product_type='".$category."' or pt1.parent_type='".$category."') types";
                        $wherePart=$wherePart." and BigP.product_type=types.product_type";
                    }
                }else{
                    if(isset($_COOKIE["category"])||isset($_GET["category"])){
                        $hasCondition=true;
                        $category="";
                        if(isset($_COOKIE["category"])){
                            $category=$_COOKIE["category"];
                        }else{
                            $category=$_GET["category"];
                        }
                        $fromPart=",(select pt1.product_type from product_types pt1 where pt1.product_type='".$category."' or pt1.parent_type='".$category."') types";
                        $wherePart=$wherePart." BigP.product_type=types.product_type";
                    }
                }
                $sql_product=$basesql_product;
                if($hasCondition){
                   $sql_product=$basesql_product.$fromPart." where ".$wherePart;
                }

                $hasCondition2=false;
                $fromPart2="";
                $wherePart2="";
                $basesql_sp="select BigSp.pid,BigSp.sid,BigSp.amount,BigSp.price from product_stores BigSp";
                if(isset($_COOKIE["pricerange"])){
                    $hasCondition2=true;
                    $temp = explode('-',$_COOKIE["pricerange"]);
                    $wherePart2=$wherePart2." BigSp.price between ".$temp[0]." and ".$temp[1];
                    if(isset($_COOKIE["store"])){
                        $wherePart2=$wherePart2." and BigSp.sid='".$_COOKIE["store"]."'";
                    }else{
                        if(isset($_COOKIE["region"])){
                            $fromPart2=",(select s1.storeid from stores s1 where s1.region='".$_COOKIE["region"]."') targets";
                            $wherePart2=$wherePart2." and BigSp.sid=targets.storeid";
                        }
                    }
                }else{
                    if(isset($_COOKIE["store"])){
                        $hasCondition2=true;
                        $wherePart2=$wherePart2." BigSp.sid='".$_COOKIE["store"]."'";
                    }else{
                        if(isset($_COOKIE["region"])){
                            $hasCondition2=true;
                            $fromPart2=",(select s1.storeid from stores s1 where s1.region='".$_COOKIE["region"]."') targets";
                            $wherePart2=$wherePart2." BigSp.sid=targets.storeid";
                        }
                    }
                }
                $sql_sp=$basesql_sp;
                if($hasCondition2){
                    $sql_sp=$basesql_sp.$fromPart2." where ".$wherePart2;
                }
                $finalsql="select p1,p2,p3,p4,p5,s.region,s.name,p7,p8 from (select sproducts1.name as p1,sproducts1.picture as p2,sproducts1.product_type as p3,sproducts1.description as p4,sproducts2.amount as p5,sproducts2.sid as p6,sproducts2.price as p7,sproducts1.PRODUCTID as p8
                    from (".$sql_product.") sproducts1,(".$sql_sp.") sproducts2 where sproducts1.productid=sproducts2.pid) results, stores s where results.p6=s.storeid limit ".$pagestart.','.$pageend .";";
                //------------------------------------------------
             }
                       $result1=mysql_query($finalsql);
                       
               while ($res1=mysql_fetch_row($result1)){
                
                     echo  '<div class="resultitem">
                           <div class="picture">
                            <img src="'.$res1[1].'">
                            </div>
                           <div class="itemDescription">
                            <div class="description_block">
                            <h4><a href = "item.php?pid= '.$res1[8].'">'.$res1[2].':'.$res1[0].' PRICE ONLY :'.$res1[7].'</a></h4>
                            <p>'.$res1[3].'
                            </p>
                            </div>
                            <div class="item_sta">
                                <p class="emphasis">store '.$res1[6] .' in '. $res1[5].' has stock amount : '.$res1[4].'</p>
                            </div>
                        </div>
                </div>';
               }  
               ?>
               
              <?php
              
                 
                echo' <div class="pageid">';
                $addtionarg="";
                if(isset($_GET['text'])||isset($_GET["category"])){
                    if(isset($_GET['text'])){
                       $addtionarg=$addtionarg."text=".$_GET['text']."&"; 
                    }
                    if(isset($_GET["category"])){
                        $addtionarg=$addtionarg."category=".$_GET['category']."&"; 
                    }
                }
                
                echo' <div class="Additem"><a href = "search.php?'.$addtionarg.'page=0-5">1</a></div>';  
                $pagesnum=0;
                
                if ($pagestart !=0&$num4>5){
                    $pagesnum = $pagestart/5;
                    echo' <div class="Additem">...</div>';
                }
                
                
                if ($num4%5!=0){
                    $i = $pagesnum;
                        while($i<=floor($num4/5)&$i<=$pagesnum+3){
                            if($i!=0)
                                echo' <div class="Additem"><a href = "search.php?'.$addtionarg.'page='.(5*$i).'-'.(5*($i+1)).'">'.($i+1).'</a></div>';  
                            $i++;
                        }
                        
                    if (!($i>floor($num4/5))&$i>$pagesnum+3)    
                        echo' <div  class="Additem">...</div>';
                  
                        while($i!= floor($num4/5)&$i>($pagesnum+3)){
                            ++$i;
                        }
                        
                    if($i== floor($num4/5))    
                        echo' <div class="Additem"><a href = "search.php?'.$addtionarg.'page='.(5*$i).'-'.(5*($i+1)).'">'.($i+1).'</a></div>';     
                }
                
                else {
                    $i = $pagesnum;
                        while($i<floor($num4/5)&$i<=$pagesnum+3){
                            if($i!=0)
                                echo' <div class="Additem"><a href = "search.php?'.$addtionarg.'page='.(5*$i).'-'.(5*($i+1)).'">'.($i+1).'</a></div>';  
                            $i++;
                        }
                        
                    if (!($i=floor($num4/5))&$i>$pagesnum+3)    
                        echo' <div  class="Additem">...</div>';
                  
                        while($i!= floor($num4/5)&$i>($pagesnum+3)){
                            ++$i;
                        }
                        
                    if($i== floor($num4/5)-1)    
                        echo '<div class="Additem"><a href = "search.php?'.$addtionarg.'page='.(5*$i).'-'.(5*($i+1)).'">'.($i+1).'</a></div>';     
                }
                
            
                
                    
                echo '<div style="clear:both;"></div>
                    </div>';
                
 
              ?>
                    
                    
                    
            </div>
            </div>
            <div style="clear:both;"></div>
        </div>
        
        

   </body>
</html>